package com.cande.loki.exercise.disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.WorkProcessor;
import com.lmax.disruptor.dsl.Disruptor;

import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @Description TODO
 * @Author wgf
 * @Date 2022/8/23 2:18 PM
 */
public class LongEventWithMethodRef {
    public static void handleEvent(LongEvent event, long sequence, boolean endOfBatch)
    {
        System.out.println(Thread.currentThread().getName() +  event.getValue());
    }

    public static void translate(LongEvent event, long sequence, ByteBuffer buffer)
    {
        event.setValue(buffer.getLong(0));
    }

    public static void main(String[] args) throws Exception
    {
        // Executor that will be used to construct new threads for consumers
        Executor executor = Executors.newCachedThreadPool();
        // Specify the size of the ring buffer, must be power of 2.
        int bufferSize = 1024;
        // Construct the Disruptor
        Disruptor<LongEvent> disruptor = new Disruptor<>(LongEvent::new, bufferSize, executor);

        // Connect the handler
//        disruptor.handleEventsWithWorkerPool(LongEventWithMethodRef::handleEvent,LongEventWithMethodRef::handleEvent);
        disruptor.handleEventsWith(LongEventWithMethodRef::handleEvent).handleEventsWith(LongEventWithMethodRef::handleEvent);
        // Start the Disruptor, starts all threads running
        disruptor.start();
        // Get the ring buffer from the Disruptor to be used for publishing.
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();


        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++)
        {
            bb.putLong(0, l);
            ringBuffer.publishEvent(LongEventWithMethodRef::translate, bb);
            Thread.sleep(1000);
        }
    }

    private static void handleEvent(LongEvent longEvent) {
        System.out.println(Thread.currentThread().getName() +  longEvent.getValue());
    }
}
